﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;


namespace MVC.Web.UI.Panel
{
    public class WebPanelBuilder :IControlBuilder<WebPanelBuilder>
    {
        private WebPanel _webPanel = null;
        private HtmlHelper _helper = null;

        public WebPanelBuilder(HtmlHelper helper) 
        {
            this._helper = helper;
            _webPanel = new WebPanel(helper.ViewContext);
        }

#region Properties
		
        public WebPanelBuilder Title(string title) 
        {
            if (!string.IsNullOrEmpty(title))
            {
                this._webPanel.Title = title;
            }
            else
            {
                throw new ArgumentException("Title cannot be null");
            }
            return this;
        }

        public WebPanelBuilder Name(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                this._webPanel.Name = name;
                this._webPanel.Attributes.Add("id", name);
            }
            else
            {
                throw new ArgumentException("Name cannot be null");
            }
            return this;
        }

        public WebPanelBuilder Content(Func<WebPanelBuilder, System.Web.WebPages.HelperResult> content) 
        {
            if (content == null)
                throw new ArgumentException("Content cannot be null");
                this._webPanel.Content = content(this);
             
            return this;
        }

        public WebPanelBuilder Width(int width) 
        {
            this._webPanel.Width =  width;
            return this;
        } 

        public WebPanelBuilder Height(int height )
        {
            this._webPanel.Height = height;
            return this;
        }

 #endregion        

        private string ConfigureScripts()
        {
            // Create tag builder
            var scriptBuilder = new TagBuilder("script");
            scriptBuilder.MergeAttribute("type", "text/javascript");

            StringBuilder innerScriptBlock = new StringBuilder();
            innerScriptBlock.Append("\n\t$(function(){");
            innerScriptBlock.Append("\n\t  $('#" + this._webPanel.Name + "').panel({");

            //"\n\t\tanimated: 'slide' -  this will be implemented on next iteration
            // "\n\t\tevent: "click"
            // innerScriptBlock.Append(     
            //     "\n\t\tactive: "+ this._webAccordion.Activate +","+
            //     "\n\t\tautoHeight: "+ this._webAccordion.AutoHeight.ToString().ToLower() +","+
            //     "\n\t\tclearStyle: " +this._webAccordion.ClearStyle.ToString().ToLower()+","+
            //     "\n\t\tcollapsible: "+ this._webAccordion.Collapsible.ToString().ToLower()+","+
            //     "\n\t\tfillSpace: "+ this._webAccordion.FillSpace.ToString().ToLower()          
            //);
            innerScriptBlock.Append("});");

            //Map all registered handlers to the client side events of the control    
            //foreach (KeyValuePair<TabEvents, string> item in this._webTab.TabEventHandlers)
            //{
            //    innerScriptBlock
            //   .Append("\n\t  $('#" + this._webTab.Name + "').bind('" + ((TabEvents)item.Key).GetStringValue() + "'," + item.Value + ")");
            //}

            innerScriptBlock.Append("\n\t});\n ");
            scriptBuilder.InnerHtml = innerScriptBlock.ToString();
            return "<!--  //-->" + "\n    " + scriptBuilder.ToString(TagRenderMode.Normal);

        }

        public void Render()
        {
            this._webPanel.Scripts = this.ConfigureScripts();
            this._webPanel.RenderControl(this._helper.ViewContext); 
        }
    }
}
